package com.example.rxjavaandretrofitdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private ProgressSubscriber.SubscriberOnNextListener<List<MovieEntity.SubjectsBean>> subscriberOnNextListener;
    @BindView(R.id.bn_click)
    Button bnClick;
    @BindView(R.id.tv_result)
    TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        subscriberOnNextListener = new ProgressSubscriber.SubscriberOnNextListener<List<MovieEntity.SubjectsBean>>() {
            @Override
            public void onNext(List<MovieEntity.SubjectsBean> subjectsBeanList) {
                String result = "";
                for (int i = 0; i < subjectsBeanList.size(); i++) {
                    result += subjectsBeanList.get(i).title + "\n";
                }
                tvResult.setText(result);
            }
        };
    }

    @OnClick(R.id.bn_click)
    public void onViewClicked() {
        getMovies5();
    }

    /**
     * 网络请求
     */
    private void getMovie() {
        String baseUrl = "https://api.douban.com/v2/movie/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MovieService service = retrofit.create(MovieService.class);

        Call<MovieEntity> call = service.getTopMovie(0, 10);

        call.enqueue(new Callback<MovieEntity>() {
            @Override
            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
                MovieEntity movieEntity = response.body();
                tvResult.setText(movieEntity.toString());
            }

            @Override
            public void onFailure(Call<MovieEntity> call, Throwable t) {
                Log.e(TAG, "onFailure: ");
            }
        });
    }

    /**
     * Rxjava和Retorfit结合
     */
    private void getMovie2() {
        String baseUrl = "https://api.douban.com/v2/movie/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())   //支持RxJava
                .build();

        final MovieService2 service = retrofit.create(MovieService2.class);

        service.getTopMovie(0, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MovieEntity>() {
                    @Override
                    public void onCompleted() {
                        Log.e(TAG, "onCompleted: ");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e(TAG, "onError: ");
                    }

                    @Override
                    public void onNext(final MovieEntity movieEntity) {
                        Log.e(TAG, "onNext: " + movieEntity);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvResult.setText(movieEntity.toString());
                            }
                        });
                    }
                });
    }

    /**
     * 使用了RxJava，并进行了封装
     */
    private void getMovies3() {
        Subscriber<MovieEntity> subscriber = new Subscriber<MovieEntity>() {
            @Override
            public void onCompleted() {
                Log.e(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                Log.e(TAG, "onError: ");
            }

            @Override
            public void onNext(final MovieEntity movieEntity) {
                Log.e(TAG, "onNext: " + movieEntity);
                tvResult.setText(movieEntity.toString());
            }
        };

        HttpMethods.getInstance().getTopMovie(subscriber, 0 , 10);
    }

    /**
     * 使用了RxJava，并进行了封装,统一了返回接口
     */
    private void getMovies4() {
        Subscriber<List<MovieEntity.SubjectsBean>> subscriber = new Subscriber<List<MovieEntity.SubjectsBean>>() {
            @Override
            public void onCompleted() {
                Log.e(TAG, "onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                Log.e(TAG, "onError: ");
            }

            @Override
            public void onNext(final List<MovieEntity.SubjectsBean> subjectsBeanList) {
                String result = "";
                for (int i = 0; i < subjectsBeanList.size(); i++) {
                    result += subjectsBeanList.get(i).title + "\n";
                }
                tvResult.setText(result);
            }
        };

        HttpMethods.getInstance().getTopMovie2(subscriber, 0 , 10);
    }

    /**
     * 封装了ProgressSubscriber
     * 在Activity或者Fragment中创建回调Listener
     */
    private void getMovies5() {
        HttpMethods.getInstance().getTopMovie2(new ProgressSubscriber(subscriberOnNextListener, this), 0 , 10);
    }
}
